﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmThongKeBanDoc : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmThongKeBanDoc()
        {
            InitializeComponent();
        }
        Utilities uti = new Utilities();
        ConnectData conn = new ConnectData();
        private void FrmThongKeBanDoc_Load(object sender, EventArgs e)
        {
            //Load combobox nhom ban doc
            DataTable tbNhom = uti.getDataSource("NhomBanDoc", "TenNhom");
            comb_MaNhom.DataSource = tbNhom;
            DataRow drLoaiSach = tbNhom.NewRow();
            drLoaiSach["MaNhom"] = "all";
            drLoaiSach["TenNhom"] = "Tất cả";
            tbNhom.Rows.InsertAt(drLoaiSach, 0);
            comb_MaNhom.DisplayMember = "TenNhom";
            comb_MaNhom.ValueMember = "MaNhom";
            comb_MaNhom.SelectedIndex = 0;
            //Load combobox TTThe
            comb_TTThe.Items.Add("Tất cả");
            comb_TTThe.Items.Add("Kích hoạt");
            comb_TTThe.Items.Add("Bị khóa");
            comb_TTThe.Items.Add("Hết hạn");
            comb_TTThe.SelectedIndex = 0;
            //
            date_Tu.Value = System.DateTime.Now;
            date_Den.Value = System.DateTime.Now;
        }

        private void btn_XemKq_Click(object sender, EventArgs e)
        {

            DataTable dtb = new DataTable();
            dtb = conn.GetDataTable(getSql(txt_TenBC.Text));
            dgv_KetQua.DataSource = dtb;
        }
        string getSql(string tenphieu)
        {
            string sql = "";
            if (txt_SoLuong.Text == "")
                sql =   "select a.MaNhom,TenNhom,b.MaBanDoc, TenBanDoc, NgaySinh,(case GioiTinh when 'true' then N'Nam' else N'Nữ' end) as GioiTinh, "+ 
                        "DiaChi,NgayLapThe, NgayHetHan, TTThe,N'"+tenphieu+"' TenBC " +
                        "from NhomBanDoc a ,BanDoc b " +
                        "where a.MaNhom=b.MaNhom ";
            else
            {
                int top = Convert.ToInt32(txt_SoLuong.Text);
                sql = "select top " + top + " a.MaNhom,TenNhom,b.MaBanDoc, TenBanDoc, NgaySinh, "+
                    "(case GioiTinh when 'true' then N'Nam' else N'Nữ' end) as GioiTinh, DiaChi, "+
                    "NgayLapThe, NgayHetHan,AnhCaNhan, TTThe,N'" + tenphieu + "' TenBC  " +
                    "from NhomBanDoc a ,BanDoc b " +
                    "where a.MaNhom=b.MaNhom ";
            }
            if (comb_MaNhom.SelectedIndex != 0)
                sql += "and a.MaNhom = '" + comb_MaNhom.SelectedValue.ToString() + "' ";
            if (comb_TTThe.SelectedIndex != 0)
                if (comb_TTThe.SelectedIndex != 3)
                    sql += "and TTThe = N'" + comb_TTThe.SelectedItem.ToString() + "' ";
                else
                    sql += "and NgayHetHan>getdate() ";
            
            //Ngày tháng
            if (check_Tu.Checked == true && check_Den.Checked == false)
                sql += "and NgayLapThe>='" + date_Tu.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_Tu.Checked == false && check_Den.Checked == true)
                sql += "and NgayLapThe <='" + date_Den.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_Tu.Checked == true && check_Den.Checked == true)
                sql += "and NgayLapThe BETWEEN '" + date_Tu.Value.ToString("yyyy-MM-dd") + "' and '" + date_Den.Value.ToString("yyyy-MM-dd") + "'";
            return sql;
        }

        private void btn_In_Click(object sender, EventArgs e)
        {
            if (txt_TenBC.Text != "")
            {
                frmReportBanDoc frm = new frmReportBanDoc();
                string title = txt_TenBC.Text.ToUpper();
                frm.dtb_BanDoc = conn.GetDataTable(getSql(title));
                frm.dk = 3;
                frm.ShowDialog();
            }
            else
                MessageBox.Show("Chưa nhập tiêu đề báo cáo", "Thông báo");
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_KetQua_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_KetQua);
        }

        private void btn_Thoat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
